﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

/// <summary>
///     BasePage 的摘要说明
/// </summary>
public class BasePage : Page
{
    public void FileDownLoad(string filename)
    {
        var destFileName = filename;

        if (File.Exists(destFileName))
        {
            var fi = new FileInfo(filename);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.Charset = "GBK";
            var browser = Context.Request.UserAgent.ToUpper();

            if (browser.Contains("MS") && browser.Contains("IE"))
            {
                Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                             + HttpUtility.UrlEncode(Path.GetFileName(destFileName), Encoding.UTF8));
            }
            else if (browser.Contains("FIREFOX"))
            {
                var outputFileName = "\"" + Path.GetFileName(destFileName) + "\"";
                Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                             + outputFileName);
            }
            else
            {
                Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                             + HttpUtility.UrlEncode(Path.GetFileName(destFileName), Encoding.UTF8));
            }

            //添加头信息，为 "文件下载/另存为 "对话框指定默认文件名  

            Response.AppendHeader("Content-Length", fi.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(destFileName);
            Response.Flush();
            Response.End();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert('下载失败，文件不存在！');", true);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginUser.Instance.ChkUserLogin();
    }
}